import { fileURLToPath, URL } from 'node:url'
import config from './src/settings.js'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig(async ({ mode }) => {
  const { VITE_BASE_PATH } = loadEnv(mode, process.cwd())
  return {
    plugins: [
      vue()
    ],
    base: VITE_BASE_PATH.replace('/', ''),
    resolve: {
      name: config.title,
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },
    build: {
      rollupOptions: {
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id
                .toString()
                .split('node_modules/')[1]
                .split('/')[0]
                .toString()
            }
          },
        },
      },
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        scss: {
          api: "modern-compiler",
          // 引入 mixin.scss 这样就可以在全局中使用 mixin.scss中预定义的变量了
          // additionalData: '@import "@/assets/mixin.scss";',
          additionalData: '@use "@/assets/mixin.scss" as *;',
        },
      },
    }
  }
})
